/**
 * 
 */
package com.runningfox.humor;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

import com.runningfox.logic.Logic;
import com.runningfox.model.Setting;

/**
 * @author luhaiyang
 * 
 */
public class Settings extends PreferenceActivity {
	String PREFS_NAME = "setting";
//	EditTextPreference namePref;
//	Preference deleteCache;
	Preference mailtoauthor,special1,special2;
	EditTextPreference usernamePref;
	EditTextPreference emailPref;
	ListPreference genderPref;
	
	Context c ;
	CheckBoxPreference networkPrefrence,themePrefrence;
	
//	TextView txtName;
//	EditText editName;
//	ImageView imgEditName,imgUpdateName,imgClose;
//	ProcessPool pool ;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.c =this.getApplicationContext();
		Logic.c =this.c;
//		Cache.DIR = c.getDir("cache", 0);
//		Cache.HOME = c.getDir("home", 0);
		this.addPreferencesFromResource(R.xml.setting);
//		this.setContentView(R.layout.setting);
		Setting.LoadSetting(this);
//		this.namePref = (EditTextPreference) this.findPreference("username");
//		this.namePref.setSummary(Setting.name);
		
//		this.deleteCache = this.findPreference("emptycache");
//		this.deleteCache.setSummary(Logic.getCacheSize()/1000+"KB");
//		this.deleteCache.setOnPreferenceClickListener(new OnPreferenceClickListener(){
//
//			@Override
//			public boolean onPreferenceClick(Preference preference) {
//				// TODO Auto-generated method stub
//				Logic.clearCache();
//				deleteCache.setSummary(Logic.getCacheSize()/1000+"KB");
//				return false;
//			}});
		this.networkPrefrence = (CheckBoxPreference) this.findPreference("network");
		this.networkPrefrence.setChecked(Setting.autoConnect);
		this.networkPrefrence.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				if(!networkPrefrence.isChecked()){
					networkPrefrence.setChecked(false);
					Setting.autoConnect=false;
					Setting.saveSetting(Settings.this);
				}else{
					networkPrefrence.setChecked(true);
					Setting.autoConnect=true;
					Setting.saveSetting(Settings.this);
				}

				return true;
			}
			
		});
		
		this.themePrefrence= (CheckBoxPreference) this.findPreference("theme");
		this.themePrefrence.setChecked(Setting.theme);
		this.themePrefrence.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				if(!themePrefrence.isChecked()){
					themePrefrence.setChecked(false);
					Setting.theme=false;
					Setting.saveSetting(Settings.this);
				}else{
					themePrefrence.setChecked(true);
					Setting.theme=true;
					Setting.saveSetting(Settings.this);
				}
				return true;
			}
			
		});
		
		this.usernamePref = (EditTextPreference)this.findPreference("username");
		this.usernamePref.setSummary(Setting.name);
		this.usernamePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				// TODO Auto-generated method stub
				Setting.name=(String)newValue;
				Setting.saveSetting(Settings.this);
				usernamePref.setSummary(Setting.name);
				return true;
			}
			
		});
		this.genderPref =(ListPreference)this.findPreference("gender");
		if(Setting.gender==1){
			genderPref.setSummary(Settings.this.getString(R.string.male));
		}else{
			genderPref.setSummary(Settings.this.getString(R.string.female));
		}
		this.genderPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				// TODO Auto-generated method stub
				Setting.gender=Integer.valueOf((String)newValue);
				Setting.saveSetting(Settings.this);
				if(Setting.gender==1){
					genderPref.setSummary(Settings.this.getString(R.string.male));
				}else{
					genderPref.setSummary(Settings.this.getString(R.string.female));
				}
				return true;
			}
			
		});
		this.emailPref = (EditTextPreference)this.findPreference("email");
		if(Setting.email==null){
			this.emailPref.setSummary(this.getString(R.string.youremailaddress));
		}else{
			this.emailPref.setSummary(Setting.email);
		}
		this.emailPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				// TODO Auto-generated method stub
				Setting.email=(String)newValue;
				Setting.saveSetting(Settings.this);
				emailPref.setSummary(Setting.email);		
				return true;
			}
			
		});
		
		
		this.special1=this.findPreference("special1");
		this.special1.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				Intent it = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.visualpharm.com/animals_icon_set/"));
				Settings.this.startActivity(it);
				return true;
			}
			
		});
		this.special2=this.findPreference("special2");
		this.special2.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				Intent it = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.kync.com/"));
				Settings.this.startActivity(it);
				return true;
			}
			
		});
		this.mailtoauthor =this.findPreference("auther");
		this.mailtoauthor.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				Intent it = new Intent(Intent.ACTION_SENDTO,Uri.parse("mailto:hitlhy@gmail.com"));
//				it.putExtra(Intent.EXTRA_SUBJECT,Setting.name+":" + this.getString(R.string.tellyouajoke));
				Settings.this.startActivity(it);
				return true;
			}
			
		});
	}
}
